<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $framework;
require_once($framework."/debug.php");
require_once($framework."/helpers/configuration.helpers.php");

/**
 * Router is the nevralgic center to route any request to the good page.
 * @author frederic delorme
 *
 */
class Router{
	private static $_currentManager;
	
	private static $_appname="framework";
	
	/**
	 * Initialize application information.
	 * @return unknown_type
	 */
	function __construct(){
		self::$_appname = __param("system","appname");
	}
	
	
	/**
	 * treat action accordingly to the url request. the request respecting the following format:
	 * http://monappli.com/?/action/entity/id 
	 * is devided in multiple variables: $action, $type and $id (if communicated).
	 */
	public function actions($request){
		global $framework;
		__debug(__CLASS__."::actions() - start :request=\"$request\"");
		$req = "";
		$req2 = "";
		$type = "";
		$id = "";
		$manager="";
		$action="";
		if(!empty($request)&& $request!="/" && 
			strchr($request,"/") && 
			substr_count($request,"/")>3){
				$size=strlen($request) - strpos($request,"index.php")-9 ;
				$request=substr($request,strpos($request,"index.php")+9,$size);
				list($req,$manager,$action,$type,$id) = explode("/",$request);
				__debug(__CLASS__."::actions() - \$request Analysed: $req,$req2,$manager,$action,$type,$id",__FILE__,__LINE__);
		}else{
			$manager="home";
			$action="list";
			__debug(__CLASS__."::actions() - default values: $req,$req2,$manager,$action",__FILE__,__LINE__);
		}
		$templateAction = $manager."/main";
		__debug(__CLASS__."::actions() => action=$action, template=".$templateAction,__FILE__,__LINE__);
		switch($action){
			// specific case for list action 'cause "list" is a PHP reserved word.
			case "list":
				$templateAction = self::getManager($manager)->lister($type,$id);
				break;

			default:
				$_manager = self::getManager($manager);
				__debug(__CLASS__."::actions - action=$action, manager=$manager",__FILE__,__LINE__);
				$_functionName=$action;
				$templateAction = $_manager->$_functionName($type,$id);					
				break;
		}
		__debug(__CLASS__."::actions() - end : templateAction=$templateAction",__FILE__,__LINE__);
		return $templateAction;
	}
	
	/**
	 * Set the default page Manager at first call.
	 * @param $defManager the default value if not specified is "home".
	 * @return none
	 */
	public function getDefaultManager(){
		__debug(__CLASS__."::getDefaultManager() - set to home",__FILE__,__LINE__);
		return "home";
	}
	
	/**
	 * Instanciate the right manager according to its name.
	 * @param $manager
	 * @return unknown_type
	 */
	protected function getManager($manager=""){
		__debug(__CLASS__."::getManager() - start manager=[$manager]",__FILE__,__LINE__);
		if($manager==""){
			$manager==self::getDefaultManager();
		}
		$fileManagerName= $manager."Manager";
		$managerName = ucfirst($fileManagerName);
		if(!is_a(self::$_currentManager,$managerName) ){
		require_once(dirname($_SERVER['SCRIPT_FILENAME'])."/webapp/managers/$fileManagerName.php");
			$_manager = new $managerName(self::$_appname);
			if(method_exists($managerName,"initialize")){				
				$_manager->initialize();
			}
			self::$_currentManager = $_manager;
		}
		__debug(__CLASS__."::getManager() - end",__FILE__,__LINE__);
		return self::$_currentManager;
	}
	
	/**
	 * return the currently loaded manager.
	 * @return unknown_type
	 */
	public function getCurrentManager(){
		if(!isset(self::$_currentManager)){
			self::getManager();
		}
		return self::$_currentManager; 
	}
}
?>